/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : event_app.js
*  Description  : 자선예약 상세
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-10  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 예약정보
	function clsEventInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsEventInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content");
			this._HTMLDom = {
				"$layer": $layer
			}
			this.setInfo({
				"시작일": ""
				, "종료일": ""
				, "당첨자발표일": ""
				, "라운드날짜": ""
				, "라운드요일": ""
				, "골프장명": ""
				, "코스명": ""
				, "홀": ""
				, "1인그린피": ""
				, "티타임": ""
				, "참여인원": ""
				, "나의상태": ""
				, "상태코드": ""
				, "총금액": 0
				, "기부금": 0
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var goal = 1000 * 1000;
			
			$layer.find("dl.event_day dd:eq(0)").html(this.infoData["시작일"] + "~" + this.infoData["종료일"]);
			$layer.find("dl.event_day dd:eq(1)").html(this.infoData["당첨자발표일"]);
			
			$layer.find("table.event_list tbody td:eq(0)").html(this.infoData["라운드날짜"] + "(" + this.infoData["라운드요일"] + ")");
			$layer.find("table.event_list tbody td:eq(1)").html("<em>" + this.infoData["골프장명"] + "</em> " + this.infoData["코스명"]);
			$layer.find("table.event_list tbody td:eq(2)").html(this.infoData["홀"]);
			$layer.find("table.event_list tbody td:eq(3)").html(_util.number_format(this.infoData["1인그린피"]));
			$layer.find("table.event_list tbody td:eq(4)").html(this.infoData["티타임"]);
			$layer.find("table.event_list tbody td:eq(5)").html(_util.number_format(this.infoData["기부금"]));
			$layer.find("table.event_list tbody td:eq(6)").html(this.infoData["참여인원"] + "<span>명</span>");
			
			//$layer.find("div.btn_area_box02").has("img[alt='당첨포기']").hide();
			
			$layer.find("img[alt='자선예약신청']").hide();
			$layer.find("table.event_list tbody td:eq(7) img").hide();
			
			var bName = "";
			
			if(this.infoData["상태코드"] == "E") bName = "신청마감";
			else if(this.infoData["나의상태코드"] == "S"){
				bName = "참여완료";
				$layer.find("div.btn_area_box02").show();
			}
			else	bName = "자선예약신청";
			
			$layer.find("img[alt='" + bName + "']").show();
			
			var p = 0;
			if(this.infoData["총금액"]){
				p = this.infoData["총금액"]*100 / goal;
			}
			$layer.find("div.graph_bar").animate({width: p+"%"}, "slow");
			$layer.find("dd.won").html("<span>\</span>" + _util.number_format(this.infoData["총금액"]));
		}
	}
	
	// 당첨자
	function clsWinner()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsWinner.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content").find("div.winner, div.btn_area_box02:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
			
			this.setInfo({
				"당첨자": ""
				, "당첨코드": ""
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			if(this.infoData["당첨자"]) this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("span.name").html(this.infoData["당첨자"]);
			if(this.infoData["당첨코드"] == 1){
				$layer.filter("div.btn_area_box02:eq(0)").show();
			}else{
				$layer.filter("div.btn_area_box02:eq(0)").hide();
			}
			
			this.show();
		}
		, show: function(){
			this._HTMLDom["$layer"].eq(0).show(300);
		}
	}
	
	// 본문탭
	function clsInfoTab()
	{
		this._HTMLDom = null;
		this.idx = null;
		
		this._init();
	}
	
	clsInfoTab.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.tabmenu");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setIdx(0);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setIdx: function(n){
			this.idx = n;
			this.display();
		}
		, getIdx: function(){
			return this.idx;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("li").attr("class", "").eq(this.idx).addClass("on");
		}
	}
	
	// 신청안내
	function clsApplyIntro()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsApplyIntro.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
		}
		, show: function(){
			this._HTMLDom["$layer"].show(300);
		}
		, hide: function(){
			this._HTMLDom["$layer"].hide(300);
		}
	}
	
	// 코스상세정보
	function clsCourseInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsCourseInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
			this.setInfo({
				"코스명": ""
				, "코스알림": ""
				, "내용": ""
				, "자세히": ""
			});
		}
		, setInfo: clsEventInfo.prototype.setInfo
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			
			$layer.find("div.detail_info:eq(0)").html(this.infoData["코스알림"]);
			
			var $tmp = $layer.find("div.detail_info:eq(1)").children().clone(true);
			$layer.find("div.detail_info:eq(1)").html(this.infoData["코스명"]).append($tmp);
			$layer.find("div.detail_golf").html(this.infoData["내용"]);
			
			if(this.infoData["자세히"]){
				$layer.find("img[alt='코스자세히보기']").show();
			}else{
				$layer.find("img[alt='코스자세히보기']").hide();
			}
		}
		, show: clsApplyIntro.prototype.show
		, hide:  clsApplyIntro.prototype.hide
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.children().hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:700px; height:100px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.children().show();
			$layer.find("div#tmpSpin").hide().spin(false);
		}
	}
	
	// 가격정보
	function clsPriceInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsPriceInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_detail:eq(2)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"그린피주중회원": 0
				, "그린피주말회원": 0
				, "그린피주중비회원": 0
				, "그린피주말비회원": 0
				, "그린피주중주중회원": 0
				, "그린피주말주중회원": 0
				, "그린피기타": 0
				, "카트비": 0
				, "캐디비": 0
				, "대여채비": 0
				, "대여화": 0
				, "코스인원제한": ""
			});
			
			$layer.hide();
		}
		, setInfo: clsEventInfo.prototype.setInfo
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("tbody tr:eq(0) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중회원"]));
			$layer.find("tbody tr:eq(0) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말회원"]));
			$layer.find("tbody tr:eq(1) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중비회원"]));
			$layer.find("tbody tr:eq(1) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말비회원"]));
			$layer.find("tbody tr:eq(2) td:eq(1) em").html(_util.number_format(this.infoData["그린피주중주중회원"]));
			$layer.find("tbody tr:eq(2) td:eq(2) em").html(_util.number_format(this.infoData["그린피주말주중회원"]));
			
			$layer.find("tbody tr:eq(3) td:eq(1) em").html(_util.number_format(this.infoData["그린피기타"]));
			$layer.find("tbody tr:eq(4) td:eq(0)").html(_util.number_format(this.infoData["카트비"]) + " 원");
			$layer.find("tbody tr:eq(5) td:eq(0)").html(this.infoData["캐디비"]);
			$layer.find("tbody tr:eq(6) td:eq(0)").html(_util.number_format(this.infoData["대여채비"]) + " 원");
			$layer.find("tbody tr:eq(7) td:eq(0)").html(_util.number_format(this.infoData["대여화"]) + " 원");
			$layer.find("tbody tr:eq(8) td:eq(0)").html(this.infoData["코스인원제한"]);
		}
		, show: clsApplyIntro.prototype.show
		, hide:  clsApplyIntro.prototype.hide
		, spinStart: clsCourseInfo.prototype.spinStart
		, spinEnd: clsCourseInfo.prototype.spinEnd
	}
	
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 상세정보
		, getEventInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "DonationDetailView";
			var options = {
				"reserveSeq": arg["예약코드"]
			}
			_gf.ajax({
				"sid": "DR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"시작일": _util.nvl(obj["startDt"])
						, "종료일": _util.nvl(obj["endDt"])
						, "당첨자발표일": _util.nvl(obj["publicDate"])
						, "라운드날짜": _util.nvl(obj["reservDate"])
						, "라운드요일": _util.nvl(obj["bookingDay"])
						, "골프장명": _util.nvl(obj["golfClubNm"])
						, "코스명": _util.nvl(obj["courseInfoNm"])
						, "홀": _util.nvl(obj["holeCnt"])
						, "1인그린피": _util.nvl(obj["greenFee"])
						, "티타임": _util.nvl(obj["bookingTime"])
						, "참여인원": _util.nvl(obj["applyCnt"], 0)
						, "총금액": _util.nvl(obj["toTalgreenFee"], 0)
						, "기부금": _util.nvl(obj["donatePrice"], 0)
						, "상태코드": _util.nvl(obj["resrvStatus"])
						, "나의상태": _util.nvl(obj["joinStatus"])
						, "나의상태코드": _util.nvl(obj["joinStatus"], "F")
					}
				}
				
				document.objControl.finishEventInfo(res);
			});
		}
		// 코스정보
		, getCourseInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "CourseInfoDetailView";
			var options = {
				"reserveSeq": arg["예약코드"]
			}
			_gf.ajax({
				"sid": "DR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"코스명": _util.nvl(obj["courseInfoNm"])
						, "코스알림": _util.nvl(obj["courseInfoNm"])
						, "내용": _util.nvl(obj["description"])
						, "자세히": ""
					}
				}
				
				document.objControl.getCourseInfo(res);
			});
		}
		// 가격정보
		, getPriceInfo: function(arg){
			var thisClass = this;

			//ajax
			var serviceName = "getGolfClubPriceInfo";
			var options = {
				"reserveSeq": arg["예약코드"]
			}
			_gf.ajax({
				"sid": "DR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"그린피주중회원": _util.nvl(obj["greenfee__weekday__member"], 0)
						, "그린피주말회원": _util.nvl(obj["grenfee__weekend__member"], 0)
						, "그린피주중비회원": _util.nvl(obj["greenfee__weekday__non__member"], 0)
						, "그린피주말비회원": _util.nvl(obj["greenfee__weekend__non__member"], 0)
						, "그린피주중주중회원": _util.nvl(obj["greenfee__weekday__weekdaymember"], 0)
						, "그린피주말주중회원": _util.nvl(obj["greenfee__weekend__weekdaymember"], 0)
						, "그린피기타": _util.nvl(obj["etcPric"], 0)
						, "카트비": _util.nvl(obj["cartfee"], 0)
						, "캐디비": _util.nvl(obj["caddiefee"])
						, "대여채비": _util.nvl(obj["rentfee"], 0)
						, "대여화": _util.nvl(obj["renteShoes"], 0)
						, "코스인원제한": _util.nvl(obj["limitMem"])
					}
				}
				
				document.objControl.finishPriceInfo(res);
			});
		}
		// 당첨자 발표
		, getWinnerInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "DonationWinnerDetailView";
			var options = {
				"reserveSeq": arg["예약코드"]
			}
			_gf.ajax({
				"sid": "DR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"당첨자": _util.nvl(obj["winnerNm"])
						, "당첨코드": _util.nvl(obj["winnerYn"])
					}
				}
				
				document.objControl.finishWinnerInfo(res);
			});
		}
		// 당첨포기
		, giveup: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "DonationWinnerCancel";
			var options = {
				"reserveSeq": arg["예약코드"]
				, "Ranking": 1
			}
			_gf.ajax({
				"sid": "DR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishGiveup(res);
			});
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objBookingControl = null;
		this.objEventInfo = null;
		this.objWinner = null;
		this.objInfoTab = null;
		this.objApplyIntro = null;
		this.objCourseInfo = null;
		this.objPriceInfo = null;
		
		this.reserveKey = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objBookingControl = new clsBookingControl();
			this.objEventInfo = new clsEventInfo();
			this.objWinner = new clsWinner();
			this.objInfoTab = new clsInfoTab();
			this.objApplyIntro = new clsApplyIntro();
			this.objCourseInfo = new clsCourseInfo();
			this.objPriceInfo = new clsPriceInfo();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			var thisClass = this;
			_gf.spinStart();
			
			this.reserveKey = _util.getParam("q_seq");
			if(!this.reserveKey){
				this.objBookingControl.goMainPage();
				return;
			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				"좌메뉴": false
			}
			
			_util.executeThread(function(){
				return thisClass.objBookingControl.isLoad();
			}, function(){
				thisClass._permitStart["좌메뉴"] = true;
			});
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;

			// 하단 탭
			this.objInfoTab.$getLayer().on("click", "a", function(){
				var n = thisClass.objInfoTab.$getLayer().find("a").index($(this));
				var cur = thisClass.objInfoTab.getIdx();
				
				if(n != cur){
					thisClass.objInfoTab.setIdx(n);
					thisClass.showTab();
				}
			});
			
			// 당첨포기
			this.objWinner.$getLayer().on("click", "img[alt='당첨포기']", function(){
				var winGiveup = window.open(_ROOT + "/popup/pop_giveup.html", "winGiveup", "width=400, height=300, scrollbars=0");
				if(winGiveup) winGiveup.focus();
			});
			
			// 예약신청
			this.objEventInfo.$getLayer().on("click", "img[alt='자선예약신청']", function(){
				if(!confirm("자선예약을 신청하시겠습니까?\n[확인]을 누르시면 결제가 진행됩니다.")) return;
				
				var winNicepay = window.open(_ROOT + "/inc/nicepay.html?q_reserve=" + thisClass.reserveKey, "winNicepay", "width=800, height=573, scrollbars=0");
				if(winNicepay) winNicepay.focus();
			});
			
			// 목록
			$("div#container section#main_content").on("click", "img[alt='목록']", function(){
				thisClass.goListPage();
			});
		}
		, _start: function(){
			this.objBookingControl._start(this, true);
			this.objBookingControl.setReserveType(1);
			
			// 상세정보 요청
			this.objComm.getEventInfo({
				"예약코드": this.reserveKey
			});
			
			// 당첨자발표
			this.objComm.getWinnerInfo({
				"예약코드": this.reserveKey
			});
			
			// 코스정보 요청
			this.objCourseInfo.spinStart();
			this.objComm.getCourseInfo({
				"예약코드": this.reserveKey
			});
			
			// 가격정보 요청
			this.objPriceInfo.spinStart();
			this.objComm.getPriceInfo({
				"예약코드": this.reserveKey
			});
			
			// 하단 페이지 설정
			this.showTab();
		}
		// 상세정보 완료
		, finishEventInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objEventInfo.setInfo(ret["결과값"]);
		}
		// 코스정보 완료
		, getCourseInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objCourseInfo.spinEnd();
			this.objCourseInfo.setInfo(ret["결과값"]);
		}
		// 가격정보 완료
		, finishPriceInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objPriceInfo.spinEnd();
			this.objPriceInfo.setInfo(ret["결과값"]);
		}
		// 당첨자 발표 완료
		, finishWinnerInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objWinner.setInfo(ret["결과값"]);
		}
		// 당첨포기 완료
		, finishGiveup: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			// 당첨자발표
			this.objComm.getWinnerInfo({
				"예약코드": this.reserveKey
			});
		}
		// 당첨 포기
		, completeGiveup: function(w){
			if(w) w.close();
			
			_gf.spinStart("sending...");
			this.objComm.giveup({
				"예약코드": this.reserveKey
			});
		}
		// 하단 페이지
		, showTab: function(){
			var a = [
				this.objApplyIntro
				, this.objCourseInfo
				, this.objPriceInfo
			];
			var n = this.objInfoTab.getIdx();
			
			for(var i = 0;i<a.length;i++){
				if(i == n) a[i].show();
				else a[i].hide();
			}
		}
		// 리스트 이동
		, goListPage: function(){
			var szLink = _ROOT + "/booking/event_list.html" + _util.paramStringFilter(["q_seq"]);
			document.location.href = szLink;
		}
		// 자선예약 완료
		, closeNicepay: function(w, ret){
			if(w) w.close();
			
			alert(ret["결과코드"]);
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			document.location.reload();
			
			//this.goReserveFinishPage();
		}
	}
	
	$.ajax({
		url: _ROOT + "/booking/js/booking_common.js"
		, type: "get"
		, dataType: "script"
		, async: false
		, success: function(ret){
			document.objControl = new clsControl();
			document.objControl._init();
		}
		, error: function(){
			alert("error");
		}
	});
});